package com.yueka.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.yueka.mapper.SystemUserMapper;
import com.yueka.pojo.R;
import com.yueka.pojo.entity.system_user.LoginInfo;
import com.yueka.pojo.entity.system_user.Password;
import com.yueka.pojo.entity.system_user.SystemUser;
import com.yueka.service.SystemUserService;
import com.yueka.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RequestMapping("/system_user")
@RestController
public class SystemUserController {

    @Resource
    private SystemUserService systemUserService;
    @Resource
    private SystemUserMapper systemUserMapper;

    @PostMapping("/login")
    public R login(@RequestBody SystemUser systemUser) {
        try {
            LoginInfo loginInfo = systemUserService.login(systemUser);
            if (loginInfo == null) {
                return R.error("用户名或密码错误！");
            }
            return R.success(loginInfo);
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    @PostMapping("/register")
    public R register(@RequestBody SystemUser systemUser) {
        try {
            return R.success(systemUserService.register(systemUser));
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    @GetMapping("/getList")
    public R getList(@RequestParam(value = "keyword", required = false) String keyword,
                     @RequestParam(value = "status", required = false) String status,
                     @RequestParam(value = "page", required = false) Integer page,
                     @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        try {
            return R.success(systemUserService.getList(keyword, status, page, pageSize));
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    @PostMapping("/update")
    public R update(@RequestBody SystemUser systemUser) {
        try {
            return R.success(systemUserService.update(systemUser));
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    @GetMapping("/delete")
    public R delete(Integer[] ids) {
        try {
            return R.success(systemUserService.delete(ids));
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    @PostMapping("/updatePersonal")
    public R updatePersonal(@RequestBody SystemUser systemUser, HttpServletRequest request) {
        try {
            systemUserService.updatePersonal(systemUser, request);
            return R.success();
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    @GetMapping("/resetPassword")
    public R resetPassword(Integer id) {
        try {
            systemUserService.resetPassword(id);
            return R.success();
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    @PostMapping("/updatePassword")
    public R updatePassword(@RequestBody Password password, HttpServletRequest request) {
        try {
            systemUserService.updatePassword(password.getOldPassword(), password.getNewPassword(), request);
            return R.success();
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    @PostMapping("/getUserInfo")
    public R<LoginInfo> getUserInfo(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        if (StrUtil.isBlank(token)) {
            return R.error("token不能为空！");
        }
        token = token.replace("Bearer ", "");
        Claims claims = JwtUtils.parseToken(token);
        Integer id = (Integer) claims.get("id");
        log.info("------  >> id = {}", id);
        if (ObjectUtil.isNull(id)) {
            return R.error("用户不存在！");
        }
        return R.success(systemUserMapper.getUserInfo(id));
    }
}
